from flask import  Flask, render_template,request,escape
import requests
import matplotlib.pyplot as plt
import json
from tianqi import weather
from dilibianma import gan



app = Flask(__name__)
def log_request(req:"flask request",res:str):
	with open("vsearch.log","a") as log:
		print(req.form,req.host_url,req.remote_addr,file=log,sep="|")



	
@app.route('/entry',methods=['post','get'])
def hello_entry():
	user = request.form.get("user")
	log_request(request,user,)
	return render_template('entry.html',the_title='天气查询')

@app.route("/login",methods=['get'])
def index()-> 'html':
	title ='welcome'
	return render_template("login.html",the_title=title)


@app.route("/tianqi",methods=["post"])
def jt():
	key='a8ead224166cf315eaa25c40689da23d'
	city='440117'
	extensions='base'
	a=weather(key,city,extensions)
	b=a['lives'][0]['city']
	c=a['lives'][0]['weather']
	d=a['lives'][0]['temperature']
	e=a['lives'][0]['winddirection']
	f=a['lives'][0]['windpower']
	g=a['lives'][0]['reporttime']
	return render_template('entry.html',city=str(b),weather=str(c),temperature=str(d),winddirection=str(e),windpower=str(f),reporttime=str(g))

@app.route("/dili",methods=["post"])
def dili():
	address="广东省广州市从化区中山大学南方学院"
	city="广州"
	h=gan(address,city)
	i=h['geocodes'][0]['location']
	return render_template('entry.html',city=str(i))




@app.route("/viewlog")
def view_log():
	contents=[]
	with open("vsearch.log","r") as log:
		for line in log:
			contents.append([])
			for item in line.split("|"):
				contents[-1].append(escape(item))
		titles= ["Formdata","Host_url","Remote_addr"]
	return render_template('view.html',the_data=contents,the_titles=titles)
	

if __name__ == '__main__':
	app.run(debug= True)